
* **************************************************************************** *
* Sierra Leone - Social Signaling and Childhood Immunization                   *
* The Combined Effects of Signals at 4 and 5 on Timely and Complete Vaccination*
* **************************************************************************** *
/*
** Purpose:    Table: The Combined Effects of Signals at 4 and 5 on Timely and 
					  Complete Vaccination

*/
********************************************************************************
********************************************************************************


* **************************************************************************** *
* 1.) Generate  a data set with the outcomes constructed according to timeliness
* **************************************************************************** *


	use  "${Replicate_SocialSignals_dtaInter}/ListingFollowUp_analysis.dta", clear


	* Keep only children that were at least 12 months age the last time we observed them:
	keep if age_pikin >= 365

	* Drop babies born in December 2017 and January 2018:
	keep if dob < td(01dec2017)


	* ----------------------------------------------------------------------------
	* Create a follow-up indicator for each vaccine based on the age of the child
	*   during the last listing:

	local    VaccineVars = " bcg penta1 penta2 penta3 measles1 "

	foreach      vaccine of local VaccineVars {
		gen     age_`vaccine'_mopup    = .
	}


	replace age_bcg_mopup      = 0            if age_listing !=. & age_listing >= 91
	replace age_bcg_mopup      = 1            if age_listing !=. & age_listing <  91

	replace age_penta1_mopup   = 0            if age_listing !=. & age_listing >= 122
	replace age_penta1_mopup   = 1            if age_listing !=. & age_listing <  122

	replace age_penta2_mopup   = 0            if age_listing !=. & age_listing >= 152
	replace age_penta2_mopup   = 1            if age_listing !=. & age_listing <  152

	replace age_penta3_mopup   = 0            if age_listing !=. & age_listing >= 183
	replace age_penta3_mopup   = 1            if age_listing !=. & age_listing <  183

	replace age_measles1_mopup = 0            if age_listing !=. & age_listing >= 350
	replace age_measles1_mopup = 1            if age_listing !=. & age_listing <  350

	gen     age_num_vaccine_mopup = age_measles1_mopup

	* ------------------------------------------------------------------------------
	* Construct outcomes according to timeliness

	* Save the vacine outcomes in seperate variables that will be used to code
	*      the completeness outcome:
	foreach vaccine in bcg penta1 penta2 penta3 measles1 {
		gen    `vaccine'_raw = `vaccine'
	}

	* Code Vaccine 1 outcome
	local a = 91

	foreach var in bcg {
		replace `var' = 0          if `var'==1 & age_`var'>=`a' & age_`var'!=.
		replace `var' = 0          if `var'==. & age_pikin>=`a' & age_pikin!=.
		replace `var' = .          if age_pikin<`a'
	}

	* Code Vaccine 2 outcome
	local b = 122

	foreach var in penta1 {
		replace `var' = 0          if `var'==1 & age_`var'>=`b' & age_`var'!=.
		replace `var' = 0          if `var'==. & age_pikin>=`b' & age_pikin!=.
		replace `var' = .          if age_pikin<`b'

		replace `var' = 0          if bcg ==0 & `var'==1
	}


	* Code Vaccine 3 outcome
	local c = 152

	foreach  var in penta2 {
		replace `var' = 0          if `var'==1 & age_`var'>=`c' & age_`var'!=.
		replace `var' = 0          if `var'==. & age_pikin>=`c' & age_pikin!=.
		replace `var' = .          if age_pikin<`c'

		replace `var' = 0          if penta1 ==0 & `var'==1
	}


	* Code Vaccine 4 outcome
	* 183 days - 6 months

	local d = 183

	foreach  var in penta3 {
		replace `var' = 0          if `var'==1 & age_`var'>=`d' & age_`var'!=.
		replace `var' = 0          if `var'==. & age_pikin>=`d' & age_pikin!=. 
		replace `var' = .          if age_pikin<`d'

		replace `var' = 0          if penta2 ==0 & `var'==1
	}


	* Code Vaccine 5 outcome
	* 350 days - 11.5 months
	local e = 350

	foreach var in measles1 {
		replace `var' = 0          if `var'==1&age_`var'>=`e'&age_`var'!=.
		replace `var' = 0          if `var'==.&age_pikin>=`e'&age_pikin!=.
		replace `var' = .          if age_pikin<`e'

		replace `var' = 0          if penta3 ==0&`var'==1
	}


	* total number of timely vaccines  
	gen     num_vaccine = .
	replace num_vaccine = 0          if bcg==0
	replace num_vaccine = 1          if bcg==1      & penta1==0
	replace num_vaccine = 2          if penta1==1   & penta2==0
	replace num_vaccine = 3          if penta2==1   & penta3==0
	replace num_vaccine = 4          if penta3==1   & measles1==0
	replace num_vaccine = 5          if measles1==1

	* ----------------------------------------------------------------------------
	* Demean control variables for this sample:

	replace good_memory = 2          if good_memory ==.
	tab     good_memory, gen(recall)

	tab     arm_anc, gen(anc)

	foreach var in  recall1 recall2 distance clinic_pop anc2 anc3 anc4 {
		sum  `var' if intervention_arm==1, detail
		gen  `var'_dm    = `var' - `r(mean)'
	}

	summ    bcg penta1 penta2 penta3 measles1

	* Combine Signal at 4 and Signal at 5:
	gen     signalS4S5 = 0
	replace signalS4S5 = 1          if inlist(intervention_arm,3,4)

	lab var signalS4S5 "Signal at 4 and 5"

	gen     panel = 1



	* Save the timely constructed data:
	save  "${Replicate_SocialSignals_dtaFinal}/ListingFollowUp_timely_analysis.dta", replace


	* **************************************************************************** *
	* 2.) Generate  a data set with the outcomes constructed according to by Age One Year
	* **************************************************************************** *


	use  "${Replicate_SocialSignals_dtaInter}/ListingFollowUp_analysis.dta", clear


	* Keep only children that were at least 12 months age the last time we observed them:
	keep if age_pikin >= 365

	* Drop babies born in December 2017 and January 2018:
	keep if dob < td(01dec2017)


	* ----------------------------------------------------------------------------
	* Create a follow-up indicator for each vaccine based on the age of the child
	*   during the last listing:

	local       VaccineVars = " vaccine1 vaccine2 vaccine3 vaccine4 vaccine5 "

	foreach      vaccine of local VaccineVars {
		gen     age_`vaccine'_mopup    = .

	}

	replace age_vaccine1_mopup = 0                if age_listing !=. & age_listing >= 365
	replace age_vaccine1_mopup = 1                if age_listing !=. & age_listing <  365

	replace age_vaccine2_mopup = 0                if age_listing !=. & age_listing >= 365
	replace age_vaccine2_mopup = 1                if age_listing !=. & age_listing <  365

	replace age_vaccine3_mopup = 0                if age_listing !=. & age_listing >= 365
	replace age_vaccine3_mopup = 1                if age_listing !=. & age_listing <  365

	replace age_vaccine4_mopup = 0                if age_listing !=. & age_listing >= 365
	replace age_vaccine4_mopup = 1                if age_listing !=. & age_listing <  365

	replace age_vaccine5_mopup = 0                if age_listing !=. & age_listing >= 365
	replace age_vaccine5_mopup = 1                if age_listing !=. & age_listing <  365

	gen     age_num_vaccine_mopup = age_vaccine5_mopup


	* ------------------------------------------------------------------------------
	* Generate Ouctome Variables and Controls:

	* Save the vacine outcomes in seperate variables that will be used to code
	*      the completeness outcome:
	foreach vaccine in bcg penta1 penta2 penta3 measles1 {
		gen    `vaccine'_raw = `vaccine'
	}


	* Code Vaccine 1 outcome
	local a = 365

	foreach  var in bcg penta1 penta2 penta3 measles1 {
		replace `var' = 0          if `var'==1 & age_`var'>=`a' & age_`var'!=.
		replace `var' = 0          if `var'==. & age_pikin>=`a' & age_pikin!=.
		replace `var' = .          if age_pikin<`a'
	}

	* Generate the cumulative number of vaccines:
	local   VaccineVars = " bcg penta1 penta2 penta3 measles1 "
	egen    vaccines    = rowtotal(`VaccineVars'), m

	foreach vaccine of numlist 1(1)5 {
		gen     vaccine`vaccine' = 0
	}
	foreach vaccine of numlist 1(1)5 {
		replace vaccine`vaccine' = 1          if vaccines >= `vaccine'
	}


	* Code the outcome variable: number of vaccines
	gen     num_vaccine = vaccines


	* --------------------------------------------------------------------------
	* Demean controls:

	tab     arm_anc, gen(anc)

	replace good_memory = 2          if good_memory ==.
	tab     good_memory, gen(recall)


	foreach var in  recall1 recall2 distance clinic_pop anc2 anc3 anc4  {
		sum    `var' if intervention_arm==1, detail
		gen    `var'_dm    = `var' - `r(mean)'
	}

	* --------------------------------------------------------------------------
	* Combine Signal at 4 and Signal at 5:
	gen     signalS4S5 = 0
	replace signalS4S5 = 1          if inlist(intervention_arm,3,4)

	lab var signalS4S5 "Signal at 4 and 5"

	gen     panel = 2


	* Svae the timely constructed data:
	save  "${Replicate_SocialSignals_dtaFinal}/ListingFollowUp_byAgeOneYear_analysis.dta", replace


	* ******************************************************************************
	* Run Regressions
	* ******************************************************************************


	* ******************************************************************************
	* 1.) Combined Effects of Signals at 4 and 5 on Timely and Complete Vaccination 
	* ******************************************************************************

	use  "${Replicate_SocialSignals_dtaFinal}/ListingFollowUp_timely_analysis.dta", clear

	append using "${Replicate_SocialSignals_dtaFinal}/ListingFollowUp_byAgeOneYear_analysis.dta"

	label var treat2       "Uninformative Bracelet"
	label var distance_dm  "Distance"

	summ      bcg penta1 penta2 penta3 measles1   if panel == 1

	summ      bcg penta1 penta2 penta3 measles1   if panel == 2

	local ANC_dm      = " anc2_dm anc3_dm anc4_dm  "
	local Controls_dm = " recall1_dm recall2_dm distance_dm clinic_pop_dm  "


	** Panel A: Effects of Signals on Timely Vaccination - Compared to Control:
	eststo clear
	
	foreach vaccine in bcg penta1 penta2 penta3 measles1 num_vaccine {

		eststo   m_`vaccine'_a:  areg `vaccine' signalS4S5  treat2  age_`vaccine'_mopup   `Controls_dm'  `ANC_dm'   if panel == 1, absorb(strata) vce(bootstrap, reps(${RepsNum}) seed(${seed}) cluster(clinic))
		test   signalS4S5 = treat2
		estadd scalar signals_treat2 = r(p)
		estadd scalar C_mean   = _b[_cons]
		estadd local  Obs      = "`e(N)'"
		estadd local  controls = "Yes"
		eststo m_`vaccine'_a_full
	}

	esttab  ///
	m_bcg_a_full m_penta1_a_full m_penta2_a_full              ///
	m_penta3_a_full m_measles1_a_full  m_num_vaccine_a_full   ///
	using "${Replicate_SocialSignals_ATables}/Table_panel_controls_new.tex",     ///
	refcat(signalS4S5 "\hline \multicolumn{1}{l}{\textbf{\textit{Panel A:}}} & \multicolumn{6}{c}{Effects of Signals on Timely Vaccination} \\ \hline %", nolabel) ///
	prehead("\begin{tabular}{l*{7}{c}} \toprule"                  ///
	"\multicolumn{1}{l}{\textbf{Dependent variable:}} & \multicolumn{1}{c}{\textbf{1 Vaccine}} & \multicolumn{1}{c}{\textbf{2 Vaccines}} & \multicolumn{1}{c}{\textbf{3 Vaccines}} & \multicolumn{1}{c}{\textbf{4 Vaccines}} & \multicolumn{1}{c}{\textbf{5 Vaccines}} & \multicolumn{1}{c}{\textbf{Total \# of vaccines}} \\")  ///
	keep(signalS4S5  treat2 distance_dm)                                  ///
	scalars("C_mean         Control Group mean"                           ///
	"Obs            Observations"                                 ///
	"signals_treat2 p(Signals = UI)"                              ///
	"controls       Controls")                                    ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))                ///
	sfmt(3) b(3) se(3)  ${StarsOpt}                               ///
	fragment nogaps nomtitles label noobs nolines                 ///
	replace


	** Panel B: Effects of Signals on Vaccination by Age One Year
	foreach vaccine in vaccine1 vaccine2 vaccine3 vaccine4 vaccine5 num_vaccine {

		eststo   model_`vaccine'_b:  areg `vaccine'  signalS4S5  treat2   age_`vaccine'_mopup `Controls_dm'  `ANC_dm'   if panel == 2, absorb(strata) vce(bootstrap, reps(${RepsNum}) seed(${seed}) cluster(clinic))
		test   signalS4S5 = treat2
		estadd scalar signals_treat2 = r(p)
		estadd scalar C_mean  = _b[_cons]
		estadd local Obs      = "`e(N)'"
		estadd local controls = "Yes"
		eststo m_`vaccine'_b_full
		
	}

	esttab  ///
	m_vaccine1_b_full m_vaccine2_b_full  m_vaccine3_b_full              ///
	m_vaccine4_b_full  m_vaccine5_b_full m_num_vaccine_b_full           ///
	using "${Replicate_SocialSignals_ATables}/Table_panel_controls_new.tex",        ///
	refcat(signalS4S5 "\hline \multicolumn{1}{l}{\textbf{\textit{Panel B:}}}  & \multicolumn{6}{c}{Effects of Signals on Vaccination by Age One Year} \\ \hline %", nolabel) ///
	keep(signalS4S5  treat2 distance_dm)                                ///
	scalars("C_mean         Control Group mean"                         ///
	"Obs            Observations"                               ///
	"signals_treat2 p(Signals = UI)"                            ///
	"controls       Controls")                                  ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))              ///
	sfmt(3) b(3) se(3)  ${StarsOpt}                                     ///
	fragment append nomtitles noobs nonumbers nolines                   ///
	postfoot("\bottomrule \end{tabular}")                               ///
	label

	filefilter "${Replicate_SocialSignals_ATables}/Table_panel_controls_new.tex"      ///
	"${Replicate_SocialSignals_ATables}/Table_EffectsOfSignals_Timely_AgeOneYear_raw.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_panel_controls_new.tex"

	filefilter "${Replicate_SocialSignals_ATables}/Table_EffectsOfSignals_Timely_AgeOneYear_raw.tex"      ///
	"${Replicate_SocialSignals_ATables}/Table_EffectsOfSignalsPooled_Timely_AgeOneYear_WithControls.tex", from("pvalue\BS_str") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_EffectsOfSignals_Timely_AgeOneYear_raw.tex"


	* ----------------------------------------------------------------------------
	* outsheet p-values, with controls


	esttab  ///
	m_bcg_a_full m_penta1_a_full m_penta2_a_full              ///
	m_penta3_a_full m_measles1_a_full  m_num_vaccine_a_full   ///
	using "${Replicate_SocialSignals_ATables}/Table_panel_controls_new.tex",     ///
	refcat(signalS4S5 "\hline \multicolumn{1}{l}{\textbf{\textit{Panel A:}}} & \multicolumn{6}{c}{Effects of Signals on Timely Vaccination} \\ \hline %", nolabel) ///
	prehead("\begin{tabular}{l*{7}{c}} \toprule"                  ///
	"\multicolumn{1}{l}{\textbf{Dependent variable:}} & \multicolumn{1}{c}{\textbf{1 Vaccine}} & \multicolumn{1}{c}{\textbf{2 Vaccines}} & \multicolumn{1}{c}{\textbf{3 Vaccines}} & \multicolumn{1}{c}{\textbf{4 Vaccines}} & \multicolumn{1}{c}{\textbf{5 Vaccines}} & \multicolumn{1}{c}{\textbf{Total \# of vaccines}} \\")  ///
	keep(signalS4S5  treat2 distance_dm)                                  ///
	scalars("C_mean         Control Group mean"                           ///
	"Obs            Observations"                                 ///
	"signals_treat2 p(Signals = UI)"                              ///
	"controls       Controls")                                    ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))                ///
	sfmt(3) b(3) p(3)  ${StarsOpt}                               ///
	fragment nogaps nomtitles label noobs nolines                 ///
	replace

	esttab  ///
	m_vaccine1_b_full m_vaccine2_b_full  m_vaccine3_b_full              ///
	m_vaccine4_b_full  m_vaccine5_b_full m_num_vaccine_b_full           ///
	using "${Replicate_SocialSignals_Tables}/Table_panel_controls_new.tex",        ///
	refcat(signalS4S5 "\hline \multicolumn{1}{l}{\textbf{\textit{Panel B:}}}  & \multicolumn{6}{c}{Effects of Signals on Vaccination by Age One Year} \\ \hline %", nolabel) ///
	keep(signalS4S5  treat2 distance_dm)                                ///
	scalars("C_mean         Control Group mean"                         ///
	"Obs            Observations"                               ///
	"signals_treat2 p(Signals = UI)"                            ///
	"controls       Controls")                                  ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))              ///
	sfmt(3) b(3) p(3)  ${StarsOpt}                                     ///
	fragment append nomtitles noobs nonumbers nolines                   ///
	postfoot("\bottomrule \end{tabular}")                               ///
	label

	filefilter "${Replicate_SocialSignals_ATables}/Table_panel_controls_new.tex"      ///
	"${Replicate_SocialSignals_ATables}/Table_EffectsOfSignals_Timely_AgeOneYear_raw.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_panel_controls_new.tex"

	filefilter "${Replicate_SocialSignals_ATables}/Table_EffectsOfSignals_Timely_AgeOneYear_raw.tex"      ///
	"${Replicate_SocialSignals_ATables}/pvalues/Table_EffectsOfSignalsPooled_Timely_AgeOneYear_WithControls_pvals.tex", from("pvalue\BS_str") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_EffectsOfSignals_Timely_AgeOneYear_raw.tex"





* **************************************************************************** *
*  End of the Dofile !!!
* **************************************************************************** *
